﻿using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace OpenQuarters.WebQuarters.Core
{
    public class ImageResult : ActionResult
    {
        public Image Image
        {
            get;
            set;
        }

        public ImageFormat ImageFormat
        {
            get;
            set;
        }

        private static Dictionary<ImageFormat, string> FormatMap
        {
            get;
            set;
        }

        static ImageResult()
        {
            CreateContentTypeMap();
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (Image == null) throw new ArgumentNullException("Image");
            if (ImageFormat == null) throw new ArgumentNullException("ImageFormat");

            Oracle.Response.Clear();
            Oracle.Response.ContentType = FormatMap[ImageFormat];

            Image.Save(Oracle.Response.OutputStream, ImageFormat);
        }

        private static void CreateContentTypeMap()
        {
            FormatMap = new Dictionary<ImageFormat, string>
      {
          { ImageFormat.Bmp,  "image/bmp"                },
          { ImageFormat.Gif,  "image/gif"                },
          { ImageFormat.Icon, "image/vnd.microsoft.icon" },
          { ImageFormat.Jpeg, "image/Jpeg"               },
          { ImageFormat.Png,  "image/png"                },
          { ImageFormat.Tiff, "image/tiff"               },
          { ImageFormat.Wmf,  "image/wmf"                }
      };
        }
    }
}
